/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.util.MLArrayUtil;

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.Dictionary;

	/**
	 * @author Marek Brun
	 */
	public class MCFramesObserver extends MLBroadcastClass {
		
		private var mlmc:MLMC;
		private static const servicedObjects : Dictionary = new Dictionary(true);
		private var watchedFrameAndMethods:Array;//of Array of [uint, Function]
		private var methodToInvokeWhenEnterNewFrame:Array;//of Array of Function
		private var countAdded:int;

		

		public function MCFramesObserver(access:Private, mc:MovieClip) {
			mlmc=MLMC.forInstance(mc);
			watchedFrameAndMethods=[];
			methodToInvokeWhenEnterNewFrame=[];
		}
		
		protected function tryRemoveListener():void {
			//TODO
		}
		
		public function addWatchForFrame(frame:int, methodToInvoke:Function):void {
			if(!watchedFrameAndMethods[frame]){
				watchedFrameAndMethods[frame]=[];
			}
			if(MLArrayUtil.addUnique(watchedFrameAndMethods[frame], methodToInvoke)){
				countAdded++;
				if(countAdded==1){
					mc.addEventListener(Event.ENTER_FRAME, onMCEnterFrame, false, 0, true);
				}
			}
		}
		
		public function removeWatchForFrame(frame:int, methodToInvoke:Function):void {
			if(watchedFrameAndMethods[frame]){
				if(MLArrayUtil.remove(watchedFrameAndMethods[frame], methodToInvoke)){
					countAdded--;
					if(!countAdded){
						mc.removeEventListener(Event.ENTER_FRAME, onMCEnterFrame);
					}
				}
			}
		}
		
		public function addWatchForEnterNewFrame(methodToInvoke:Function):void {
			MLArrayUtil.addUnique(methodToInvokeWhenEnterNewFrame, methodToInvoke);
		}
		
		public function removeWatchForEnterNewFrame(methodToInvoke:Function):void {
			MLArrayUtil.remove(methodToInvokeWhenEnterNewFrame, methodToInvoke);
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
		public static function forInstance(mc:MovieClip):MCFramesObserver {
			if(servicedObjects[mc]){
				return servicedObjects[mc];
			}else{
				servicedObjects[mc]=new MCFramesObserver(null, mc);
				return servicedObjects[mc];
			}
		}
		
//********************************************************************************************
//		events for MCFramesObserver
//********************************************************************************************
		protected function onMCEnterFrame(event:Event):void {
			if(watchedFrameAndMethods[mc.currentFrame]){
				var funcs:Array=watchedFrameAndMethods[mc.currentFrame];
				var i:uint;
				var loopFunc:Function;
				for(i=0;i<funcs.length;i++){
					loopFunc=funcs[i];
					loopFunc();
				}
			}
		}
		
	}
}
internal class Private {}